package com.soujiayi.adapter;

import java.util.List;

import com.soujiayi.activity.R;
import com.soujiayi.model.Product;
import com.soujiayi.model.ProductCompare;
import com.soujiayi.net.ImageLoader;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Product> data;
	private ImageLoader imageLoader;
	private String mall_number = "";
	private String price_unit = "";
	private int width;//屏宽
	private int height;//屏高

	public ProductListAdapter(Context context,
			List<Product> data) {
		this.data = data;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader=new ImageLoader(context.getApplicationContext(),true,100,100,3);
		
		this.mall_number = context.getResources().getString(R.string.product_detail_mall_number);
		this.price_unit = context.getResources().getString(R.string.price_unit);
		
		DisplayMetrics dm = new DisplayMetrics();  
		dm = context.getResources().getDisplayMetrics();
		width = (int) (100*dm.density);
		height = (int) (100*dm.density);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		
		View view = convertView;
		ViewHolder holder; 
		if(view == null){
			view = mInflater.inflate(R.layout.product_list_item, null);
			holder = new ViewHolder();
			holder.productImg = (ImageView)view.findViewById(R.id.product_list_productImg);
			holder.productPrice = (TextView)view.findViewById(R.id.product_list_productPrice);
			holder.productName = (TextView)view.findViewById(R.id.product_list_productName);
			holder.productSeller = (TextView)view.findViewById(R.id.product_list_productSeller);
			holder.productSellerCount = (TextView)view.findViewById(R.id.product_list_productSellerCount);
			view.setTag(holder);
		}else{
			holder = (ViewHolder)view.getTag(); 
		}
		Product item = data.get(position);
		bindView(holder, item);

		return view;
	}

	private void bindView(ViewHolder holder, Product item) {
		String productName = item.getName();
		if(productName != null){
			if(productName.length() > 50){
				productName = productName.substring(0, 50) + "...";
			}
		}
		holder.productName.setText(productName == null ? "" : productName);
		String productPrice = item.getSaleprice();
		holder.productPrice.setText(productPrice == null ? "" : productPrice + this.price_unit);

		
		ProductCompare compare = item.getCompare();
		if(compare != null && compare.getMallnum() > 1){
			int productSellerCount = compare.getMallnum();
			if(productSellerCount == 0){
				holder.productSellerCount.setText("");
				String productSeller = item.getMallname();
				holder.productSeller.setText(productSeller == null ? "" : productSeller);
			}else{
				holder.productSeller.setText("");
				holder.productSellerCount.setText(productSellerCount + mall_number);
			}
		}else{
			holder.productSellerCount.setText("");
			String productSeller = item.getMallname();
			holder.productSeller.setText(productSeller == null ? "" : productSeller);
		}
		
		holder.productImg.setDrawingCacheEnabled(true);
		if(item.getCover() != null){
			String imageUrl = item.getCover().replace(".jpg","." + width + "x" + height + ".jpg");
			imageLoader.DisplayImage(imageUrl,holder.productImg);
		}
	}

	static class ViewHolder {
		TextView productName, productPrice, productSeller, productSellerCount;
		ImageView productImg;
	}
	
	public void close(){
		imageLoader.close();
	}
}
